#include "system.h"
struct flapp_dis cur_dis_content;
struct flapp_dis last_dis_content;

const uint8_t flapp_ascll_buf[10] = {ASC_0,ASC_1,ASC_2,ASC_3,ASC_4,ASC_5,ASC_6,ASC_7,ASC_8,ASC_9};
/**********************************************************
函数功能：显示初始化
入口参数：无
出口参数：无
说	  明 
***********************************************************/ 
void flapp_display_init(void)
{
	cur_dis_content.flag_test = FALSE;
	cur_dis_content.flag_auto = TRUE;
	cur_dis_content.flag_ion  = TRUE;
	cur_dis_content.flag_filter = FALSE;
	last_dis_content.flag_filter = TRUE;
	cur_dis_content.flag_net  = FALSE;
	cur_dis_content.flag_temp = FALSE;
	cur_dis_content.value_fan = AIR_GENERAL;
	cur_dis_content.value_air[0] = FALSE;
	cur_dis_content.air_level = AIR_GENERAL;
	last_dis_content.air_level = AIR_NOT;
	cur_dis_content.voc_count_down = DEFAULT_COUNT_DOWN;
}


void flapp_test_display(void)
{
	static uint8_t test_page = PAGE_NUM_MAX;
	uint8_t send_dis_buf[DIS_LEN_AIR_GROUP + 3] = {DIS_SEND_HEAH_1,DIS_SEND_HEAH_2,0,DIS_CMD_WR};
	struct flapp_tmr get_tmr_content;
	get_tmr_content = flapp_get_tmr_status();
	switch(test_page)
	{
		case PAGE_NUM_MAX: 
		{
			test_page = PAGE_TEST_MODE;
			send_dis_buf[DIS_DATA_LEN] = DIS_LEN_TEST;
			send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(TEST_ICON_VERSION >> 8);				//切页最后切，切页字节与写字节数据不同步
			send_dis_buf[DIS_ADDR_L]	 = (uint8_t)TEST_ICON_VERSION;
			send_dis_buf[DIS_DATA1_L] = DIS_PASS;
			send_dis_buf[DIS_DATA2_H] = ASC_V;
			send_dis_buf[DIS_DATA2_L] = flapp_ascll_buf[SYS_VERSION / 10];
			send_dis_buf[DIS_RES2_H]  = flapp_ascll_buf[SYS_VERSION % 10];
			fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_TEST + 3);
			
			send_dis_buf[DIS_DATA_LEN] = DIS_CUT_PAGE;
			send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(DIS_ADDR_CUT_PAGE >> 8);				//切页最后切，切页字节与写字节数据不同步
			send_dis_buf[DIS_ADDR_L]	 = (uint8_t)DIS_ADDR_CUT_PAGE;
			send_dis_buf[DIS_DATA1_H]  = DIS_CMD_CUT_ST;
			send_dis_buf[DIS_DATA1_L]	 = DIS_CMD_CUT_MODE;
			send_dis_buf[DIS_RES1_L]	 = PAGE_TEST_MODE;
			fldrv_uart_send_buf(USART3,send_dis_buf,DIS_CUT_PAGE + 3);
			break;
		}
		case PAGE_TEST_MODE:
		{
			if((xQueueReceive(air_quality_queue,cur_dis_content.value_air,0) == pdTRUE) && \
			memcmp(cur_dis_content.value_air,last_dis_content.value_air,sizeof(cur_dis_content.value_air)))
			{
				send_dis_buf[DIS_DATA_LEN] = DIS_LEN_FAN;
				send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(TEST_ICON_VOC >> 8);			
				send_dis_buf[DIS_ADDR_L]	 = (uint8_t)TEST_ICON_VOC;
				send_dis_buf[DIS_DATA1_L] = DIS_PASS;
				send_dis_buf[DIS_DATA2_H] = flapp_ascll_buf[cur_dis_content.value_air[1] / 100];
				send_dis_buf[DIS_DATA2_L] = flapp_ascll_buf[cur_dis_content.value_air[1] % 100 / 10];
				send_dis_buf[DIS_RES2_H]  = flapp_ascll_buf[cur_dis_content.value_air[1] % 10];
//				send_dis_buf[DIS_DATA2_H] = ASC_O;
//				send_dis_buf[DIS_DATA2_L] = ASC_K;
				fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_FAN + 3);
				
				send_dis_buf[DIS_DATA_LEN] = DIS_LEN_TEST;
				send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(TEST_ICON_PM25 >> 8);			
				send_dis_buf[DIS_ADDR_L]	 = (uint8_t)TEST_ICON_PM25;
				send_dis_buf[DIS_DATA1_L] = DIS_PASS;
				send_dis_buf[DIS_DATA2_H] = flapp_ascll_buf[cur_dis_content.value_air[0] / 100];
				send_dis_buf[DIS_DATA2_L] = flapp_ascll_buf[cur_dis_content.value_air[0] % 100 / 10];
				send_dis_buf[DIS_RES2_H]  = flapp_ascll_buf[cur_dis_content.value_air[0] % 10];
				fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_TEST + 3);
				if(cur_dis_content.value_air[0] > PM25_BAD || cur_dis_content.value_air[1] > VOC_BAD){
				cur_dis_content.air_level = AIR_BAD;
				}
				else if(cur_dis_content.value_air[0] > PM25_GEN || cur_dis_content.value_air[1] > VOC_GEN){
					cur_dis_content.air_level = AIR_GENERAL;
				}
				else{
					cur_dis_content.air_level = AIR_GOOD;
				}
				
				tmr_channel_value_set(TMR17, TMR_SELECT_CHANNEL_1, (FAN_PWM_CTR(cur_dis_content.air_level)) * 38.5);
				/* output enable */
				tmr_output_enable(TMR17, TRUE);
				/* tmr enable counter */
				tmr_counter_enable(TMR17, TRUE);
			}
			
			if((xQueueReceive(t_h_queue,cur_dis_content.val_t_h_buf,0) == pdTRUE) && \
			memcmp(cur_dis_content.val_t_h_buf,last_dis_content.val_t_h_buf,sizeof(cur_dis_content.val_t_h_buf)))
			{
				send_dis_buf[DIS_DATA_LEN] = DIS_LEN_VOC;
				send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(TEST_ICON_TEMP >> 8);			
				send_dis_buf[DIS_ADDR_L]	 = (uint8_t)TEST_ICON_TEMP;
				send_dis_buf[DIS_DATA1_L] = DIS_PASS;
				send_dis_buf[DIS_DATA2_H] = flapp_ascll_buf[cur_dis_content.val_t_h_buf[0] / 10];
				send_dis_buf[DIS_DATA2_L] = flapp_ascll_buf[cur_dis_content.val_t_h_buf[0] % 10];
				send_dis_buf[DIS_DATA3_L] = DIS_PASS;
				send_dis_buf[DIS_DATA4_H] = flapp_ascll_buf[cur_dis_content.val_t_h_buf[1] / 10];
				send_dis_buf[DIS_DATA4_L] = flapp_ascll_buf[cur_dis_content.val_t_h_buf[1] % 10];
				fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_VOC + 3);
			}
			
			send_dis_buf[DIS_DATA_LEN] = DIS_LEN_TEST;
			send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(TEST_ICON_FAN_LOW >> 8);			
			send_dis_buf[DIS_ADDR_L]	 = (uint8_t)TEST_ICON_FAN_LOW;
			send_dis_buf[DIS_DATA1_L] = DIS_PASS;
			send_dis_buf[DIS_DATA2_H] = flapp_ascll_buf[get_tmr_content.fan_speed / 1000];
			send_dis_buf[DIS_DATA2_L] = flapp_ascll_buf[get_tmr_content.fan_speed % 1000 / 100];
			send_dis_buf[DIS_RES2_H]  = flapp_ascll_buf[get_tmr_content.fan_speed % 100 / 10];
			send_dis_buf[DIS_RES2_L]  = flapp_ascll_buf[get_tmr_content.fan_speed % 10];
			fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_TEST + 3);
		}
		case PAGE_TEST_FINISH:
		{
			
			break;
		}
	}
	last_dis_content = cur_dis_content;				//只有数值变化才做更新
}
/**********************************************************
函数功能：显示任务运行函数
入口参数：无
出口参数：无
说	  明 
***********************************************************/
void flapp_dis_run(void)
{
	uint8_t sync_time = FALSE;
	uint8_t remote_rcv_buf[REMOTE_NUM];
	uint8_t send_dis_buf[DIS_LEN_AIR_GROUP + 3] = {DIS_SEND_HEAH_1,DIS_SEND_HEAH_2,0,DIS_CMD_WR};
	static uint8_t air_err = FALSE;
	if(cur_dis_content.flag_test)
	{
		flapp_test_display();
		return;
	}

	if(xQueueReceive(remote_con_queue,remote_rcv_buf,0) == pdTRUE)
	{
		cur_dis_content.flag_net = GET_BIT(remote_rcv_buf[REMOTE_VAILD],VAILD_SWITCH_NET);
		
		if(GET_BIT(remote_rcv_buf[REMOTE_VAILD],VAILD_SWITCH_AUTO))			//远程设置模式
		{
			cur_dis_content.flag_auto = remote_rcv_buf[REMOTE_AUTO];			
		}
		if(GET_BIT(remote_rcv_buf[REMOTE_VAILD],VAILD_SWITCH_ION))			//远程设置离子
		{
			cur_dis_content.flag_ion = remote_rcv_buf[REMOTE_ION];	
			if(cur_dis_content.flag_ion)	at32_gpio_high(EN_ION);
			else													at32_gpio_low(EN_ION);
		}
		if(GET_BIT(remote_rcv_buf[REMOTE_VAILD],VAILD_SWITCH_FAN))			//远程设置风速
		{
			cur_dis_content.value_fan = remote_rcv_buf[REMOTE_FAN_SPEED];	
		}
		if(GET_BIT(remote_rcv_buf[REMOTE_VAILD],VAILD_SWITCH_FIL_RST))	//远程滤网
		{
			cur_dis_content.flag_filter = remote_rcv_buf[REMOTE_FILTER_RESET];
		}
		if(GET_BIT(remote_rcv_buf[REMOTE_VAILD],VAILD_SWITCH_FIL_TMR))	//滤网寿命到
		{
			cur_dis_content.flag_filter = TRUE;
		}
	}
	
	if(cur_dis_content.voc_count_down)
	{
		uint16_t change_count_down;
		cur_dis_content.voc_count_down--;
		if(cur_dis_content.voc_count_down <= TRUE)
		{	
			cur_dis_content.voc_count_down = FALSE;
			last_dis_content.air_level = AIR_NOT;
		}
		else	//倒计时接收不显示
		{
			change_count_down = cur_dis_content.voc_count_down >> 1;
			send_dis_buf[DIS_DATA_LEN] = DIS_LEN_VOC;
			send_dis_buf[DIS_ADDR_H] = (uint8_t)(DIS_ADDR_VOC >> 8);
			send_dis_buf[DIS_ADDR_L] = (uint8_t)DIS_ADDR_VOC;
			send_dis_buf[DIS_DATA1_L] = change_count_down / 100;
			send_dis_buf[DIS_DATA2_L] = change_count_down % 100 / 10;
			send_dis_buf[DIS_DATA3_L] = change_count_down % 10;
			send_dis_buf[DIS_DATA4_L] = FALSE;
			fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_VOC + 3);	
			sync_time++;
		}	
	}
	
	if(((xQueuePeek(air_quality_queue,cur_dis_content.value_air,0) == pdTRUE) && \
		memcmp(cur_dis_content.value_air,last_dis_content.value_air,sizeof(cur_dis_content.value_air))) || \
			last_dis_content.air_level == AIR_NOT)//空气质量
	{
		send_dis_buf[DIS_ADDR_H] = (uint8_t)(DIS_ADDR_PM25 >> 8);
		send_dis_buf[DIS_ADDR_L] = (uint8_t)DIS_ADDR_PM25;
		send_dis_buf[DIS_DATA1_L] = cur_dis_content.value_air[0] / 100;
		send_dis_buf[DIS_DATA2_L] = cur_dis_content.value_air[0] % 100 / 10;
		send_dis_buf[DIS_DATA3_L] = cur_dis_content.value_air[0] % 10;
		send_dis_buf[DIS_DATA4_L] = cur_dis_content.value_air[1] / 100;
		send_dis_buf[DIS_DATA5_L] = cur_dis_content.value_air[1] % 100 / 10;
		send_dis_buf[DIS_DATA6_L] = cur_dis_content.value_air[1] % 10;
		if(cur_dis_content.voc_count_down)		//倒计时
		{
			send_dis_buf[DIS_DATA_LEN] = DIS_LEN_NUM;
			fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_NUM + 3);					//PM2.5
			sync_time++;
		}
		else
		{
			send_dis_buf[DIS_DATA_LEN] = DIS_LEN_AIR_GROUP;
			send_dis_buf[DIS_DATA7_L] = TRUE;
			fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_AIR_GROUP + 3);		//PM2.5 and VOC
			sync_time++;
			if(cur_dis_content.value_air[0] > PM25_BAD || cur_dis_content.value_air[1] > VOC_BAD){
				cur_dis_content.air_level = AIR_BAD;
			}
			else if(cur_dis_content.value_air[0] > PM25_GEN || cur_dis_content.value_air[1] > VOC_GEN){
				cur_dis_content.air_level = AIR_GENERAL;
			}
			else{
				cur_dis_content.air_level = AIR_GOOD;
			}
			if(cur_dis_content.flag_auto)
				cur_dis_content.value_fan = cur_dis_content.air_level;	//自动模式下风速和表情对应
		}
	}
	if(xQueueReceive(air_quality_queue,cur_dis_content.value_air,0) == pdFALSE)
	{
		air_err++;
		if(air_err > ERR_PM_VOC_COUNT)
		{
			air_err = ERR_PM_VOC_COUNT;
			flapp_set_master_err(TRUE,DEVICE_ERROR_VOC);
			flapp_set_master_err(TRUE,DEVICE_ERROR_PM25);
		}
	}
	else
	{
		air_err = FALSE;
		flapp_set_master_err(FALSE,DEVICE_ERROR_VOC);
		flapp_set_master_err(FALSE,DEVICE_ERROR_PM25);
	}
	
	
	
	if((xQueueReceive(t_h_queue,cur_dis_content.val_t_h_buf,0) == pdTRUE) && \
		memcmp(cur_dis_content.val_t_h_buf,last_dis_content.val_t_h_buf,sizeof(cur_dis_content.val_t_h_buf)))//温湿度
	{
		if(cur_dis_content.val_t_h_buf[0] < 0)
		{
			cur_dis_content.flag_temp = TRUE;
			cur_dis_content.val_t_h_buf[0] = ~cur_dis_content.val_t_h_buf[0] + 1;	//温度为负数取反加一
		}
			
		send_dis_buf[DIS_DATA_LEN] = DIS_LEN_TMP_GROUP;
		send_dis_buf[DIS_ADDR_H] = (uint8_t)(DIS_ADDR_TEMP >> 8);
		send_dis_buf[DIS_ADDR_L] = (uint8_t)DIS_ADDR_TEMP;
		send_dis_buf[DIS_DATA1_L] = cur_dis_content.flag_temp;
		send_dis_buf[DIS_DATA2_L] = cur_dis_content.val_t_h_buf[0] / 10;
		send_dis_buf[DIS_DATA3_L] = cur_dis_content.val_t_h_buf[0] % 10;
		send_dis_buf[DIS_DATA4_L] = cur_dis_content.val_t_h_buf[1] / 100;
		send_dis_buf[DIS_DATA5_L] = cur_dis_content.val_t_h_buf[1] % 100 / 10;
		send_dis_buf[DIS_DATA6_L] = cur_dis_content.val_t_h_buf[1] % 10;
		fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_TMP_GROUP + 3);		
		sync_time++;
	}
	
	if(EQUAL_VALUE(cur_dis_content.flag_auto,last_dis_content.flag_auto))	//自动图标
	{
		send_dis_buf[DIS_DATA_LEN] = DIS_LEN_DEFAULT;
		send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(DIS_ADDR_AUTO >> 8);
		send_dis_buf[DIS_ADDR_L]	 = (uint8_t)DIS_ADDR_AUTO;
		send_dis_buf[DIS_DATA1_L]	 = cur_dis_content.flag_auto;
		fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_DEFAULT + 3);
		sync_time++;
	}
	
	if(EQUAL_VALUE(cur_dis_content.flag_ion,last_dis_content.flag_ion))	//离子图标
	{
		send_dis_buf[DIS_DATA_LEN] = DIS_LEN_DEFAULT;
		send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(DIS_ADDR_ION >> 8);
		send_dis_buf[DIS_ADDR_L]	 = (uint8_t)DIS_ADDR_ION;
		send_dis_buf[DIS_DATA1_L]	 = cur_dis_content.flag_ion;
		fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_DEFAULT + 3);
		sync_time++;
	}
	
	if(EQUAL_VALUE(cur_dis_content.flag_filter,last_dis_content.flag_filter))	//滤网图标
	{
		send_dis_buf[DIS_DATA_LEN] = DIS_LEN_DEFAULT;
		send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(DIS_ADDR_FIL >> 8);
		send_dis_buf[DIS_ADDR_L]	 = (uint8_t)DIS_ADDR_FIL;
		send_dis_buf[DIS_DATA1_L]	 = cur_dis_content.flag_filter;
		fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_DEFAULT + 3);
		sync_time++;
	}
	
	if(EQUAL_VALUE(cur_dis_content.flag_net,last_dis_content.flag_net))	//网络图标
	{
		send_dis_buf[DIS_DATA_LEN] = DIS_LEN_DEFAULT;
		send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(DIS_ADDR_NET >> 8);
		send_dis_buf[DIS_ADDR_L]	 = (uint8_t)DIS_ADDR_NET;
		send_dis_buf[DIS_DATA1_L]	 = cur_dis_content.flag_net;
		fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_DEFAULT + 3);
		sync_time++;
	}
	
	if(EQUAL_VALUE(cur_dis_content.value_fan,last_dis_content.value_fan))	//风速图标
	{
		tmr_channel_value_set(TMR17, TMR_SELECT_CHANNEL_1, (FAN_PWM_CTR(cur_dis_content.value_fan)) * 38.5);
				/* output enable */
		tmr_output_enable(TMR17, TRUE);
		/* tmr enable counter */
		tmr_counter_enable(TMR17, TRUE);
		send_dis_buf[DIS_DATA_LEN] = DIS_LEN_FAN;
		send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(DIS_ADDR_FAN >> 8);
		send_dis_buf[DIS_ADDR_L]	 = (uint8_t)DIS_ADDR_FAN;
		send_dis_buf[DIS_DATA1_L]	 = cur_dis_content.value_fan;
		send_dis_buf[DIS_DATA2_L]	 = cur_dis_content.value_fan;
		fldrv_uart_send_buf(USART3,send_dis_buf,DIS_LEN_FAN + 3);
		sync_time++;
	}
	
	if(EQUAL_VALUE(cur_dis_content.air_level,last_dis_content.air_level))	//页面切换
	{
		send_dis_buf[DIS_DATA_LEN] = DIS_CUT_PAGE;
		send_dis_buf[DIS_ADDR_H]	 = (uint8_t)(DIS_ADDR_CUT_PAGE >> 8);				//切页最后切，切页字节与写字节数据不同步
		send_dis_buf[DIS_ADDR_L]	 = (uint8_t)DIS_ADDR_CUT_PAGE;
		send_dis_buf[DIS_DATA1_H]  = DIS_CMD_CUT_ST;
		send_dis_buf[DIS_DATA1_L]	 = DIS_CMD_CUT_MODE;
		send_dis_buf[DIS_RES1_L]	 = cur_dis_content.air_level + 1;
		fldrv_uart_send_buf(USART3,send_dis_buf,DIS_CUT_PAGE + 3);
		sync_time++;
	}
	last_dis_content = cur_dis_content;				//只有数值变化才做更新
	vTaskDelay(TASK_DISPLAY_DLY - sync_time * (TASK_UART_SEND_DLY - 6));
}

/**********************************************************
函数功能：获取显示接口结构体
入口参数：无
出口参数：flapp_dis
说	  明 
***********************************************************/ 
struct flapp_dis flapp_get_device_status(void)
{
	return last_dis_content;
}
/**********************************************************
函数功能：获取主机自检
入口参数：无
出口参数：u8
说	  明 
***********************************************************/ 
uint8_t flapp_get_master_err_bit(uint8_t bit)
{

	return GET_BIT(cur_dis_content.master_err,bit);
}
/**********************************************************
函数功能：获取从机自检
入口参数：无
出口参数：u8
说	  明 
***********************************************************/ 
uint8_t flapp_get_slave_err_bit(uint8_t bit)
{
	return GET_BIT(cur_dis_content.slave_err,bit);
}
/**********************************************************
函数功能：设置主机自检
入口参数：flag，value
出口参数：无
说	  明 
***********************************************************/ 
void flapp_set_master_err(uint8_t flag,uint8_t value)
{
	uint8_t device_mark;
	device_mark = flapp_get_device_type();
	switch(device_mark)
	{
		case DEVICE_UNKNOWN:
			break;
		case DEVICE_MASTER:
				if(flag)
					SET_BIT(cur_dis_content.master_err,value);
				else
					CLR_BIT(cur_dis_content.master_err,value);
			break;
		case DEVICE_SLAVE:
				if(flag)
					SET_BIT(cur_dis_content.slave_err,value);
				else
					CLR_BIT(cur_dis_content.slave_err,value);
			break;
	}
}
/**********************************************************
函数功能：设置从机地址
入口参数：uint8_t addr
出口参数：无
说	  明 
***********************************************************/ 
void flapp_set_slave_addr(uint8_t addr)
{
	cur_dis_content.slave_addr = addr;
}

/**********************************************************
函数功能：设置从机地址
入口参数：uint8_t addr
出口参数：无
说	  明 
***********************************************************/ 
void flapp_set_slave_err_report(uint16_t err_report)
{
	cur_dis_content.slave_err_report = err_report;
}
